package com.cheng.rocket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.mapper.SummaryPlugMapper;
import com.cheng.pojo.po.Summary;
import com.cheng.pojo.vo.RocketStatus;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static java.lang.Thread.sleep;

@Service
@RocketMQMessageListener(
        topic = "summary",
        selectorExpression = "status",
        consumerGroup = "JXGroup-summary-status"
)
public class StatusListener implements RocketMQListener<RocketStatus> {

    @Resource
    private SummaryPlugMapper summaryPlugMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void onMessage(RocketStatus rocketStatus) {
        Summary summary = summaryPlugMapper.selectOne(
                new QueryWrapper<Summary>()
                        .eq("year", rocketStatus.getYear())
                        .eq("unit", rocketStatus.getUnit()));

        stringRedisTemplate.delete("summary:edit:"+rocketStatus.getYear());
        int i = summaryPlugMapper.updateById(new Summary().builder().id(summary.getId()).status(rocketStatus.getStatus()).build());
        if(i == 1){
            try{
                sleep(500);
            }catch(Exception e){
                System.out.println("休眠中出错");
            }
            stringRedisTemplate.delete("summary:edit:"+rocketStatus.getYear());
        }
    }
}